package com.vastcom.tyauthcenter.action;

import com.vastcom.tyauthcenter.model.Loginlog;
import com.vastcom.tyauthcenter.model.Loginticket;
import com.vastcom.tyauthcenter.model.Member;
import com.vastcom.tyauthcenter.service.ManageService;
import com.vastcom.tyauthcenter.tools.JwtUtil;
import com.vastcom.tyauthcenter.tools.Tools;
import com.vastcom.tyauthcenter.vo.PageParmVo;
import eu.bitwalker.useragentutils.UserAgent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.util.HashMap;

@CrossOrigin
@RestController
@RequestMapping("/public")

public class publicAction {


    @Autowired
    private ManageService manageService;

    @PostMapping("/test")
    @ResponseBody
    public HashMap<String, String> test(
            @RequestBody Member member) throws Exception {
        HashMap hashMap = new HashMap();
        hashMap.put("msg", "ok");
        return hashMap;
    }

    @PostMapping("/login")
    @ResponseBody
    public HashMap<String, String> login(HttpServletRequest request,
            @RequestBody Member member) throws Exception {
        try {
            Member u = manageService.getMemberByUserNameAndPass(member.getName(), member.getPass());
            if (u != null) {
                if (u.getState().equals("NORMAL")) {

//                    设置登录日志
                    UserAgent userAgent = UserAgent.parseUserAgentString(request.getHeader("User-Agent"));
                    Loginlog loginlog = new Loginlog();
                    String ip = Tools.getIpAddr(request);
                    loginlog.setUserid(u.getMemberid());
                    loginlog.setUsername(u.getName());
                    loginlog.setIp(ip);
                    loginlog.setOs(userAgent.getOperatingSystem().toString());
                    loginlog.setBrowser(userAgent.getBrowser().toString());
                    loginlog.setLogintime( new Timestamp(System.currentTimeMillis()));
                    manageService.saveLoginlog(loginlog);
//                  生成token
                    String jwt = JwtUtil.generateToken("member", u.getMemberid());
                    return new HashMap<String, String>() {{
                        put("msg", "ok");
                        put("token", jwt);
                        put("role", "member");
                    }};



                } else if (u.getState().equals("LOCKED")) {
                    return new HashMap<String, String>() {{
                        put("msg", "locked");

                    }};
                } else {
                    return new HashMap<String, String>() {{
                        put("msg", "error");

                    }};
                }


            } else {
                //return new ResponseEntity(HttpStatus.UNAUTHORIZED);
                return new HashMap<String, String>() {{
                    put("msg", "errorpass");

                }};
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new HashMap<String, String>() {{
                put("msg", "error");

            }};
        }
    }


    @PostMapping("loginByTicket")
    @ResponseBody
    public HashMap loginByTicket(@RequestBody PageParmVo pageParmVo) {
        HashMap hashMap = new HashMap();
        try {
            Loginticket loginticketBYid = manageService.getLoginticketBYid(pageParmVo.getParm());
            if (loginticketBYid == null) {
                hashMap.put("msg", "errorparm");
            } else {
                long temp = new Timestamp(System.currentTimeMillis()).getTime();
                if (loginticketBYid.getVaildtime().getTime() < temp) {
                    //过期
                    hashMap.put("msg", "expriss");
                } else {
                    //有效
                    hashMap.put("msg", "ok");
                    hashMap.put("token", loginticketBYid.getToken());
                }
            }
        } catch (Exception e) {
            hashMap.put("msg", "error");
            e.printStackTrace();
        }

        return hashMap;
    }

}
